package com.cloudeasy.post.service.expand;

import com.cloudeasy.api.constant.AmqpConstant;
import com.cloudeasy.api.util.JsonUtil;
import com.cloudeasy.api.vo.amqp.AreaItemIncVO;
import com.cloudeasy.api.vo.amqp.LabelItemIncVO;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AmqpService {
    private final AmqpTemplate amqpTemplate;

    @Autowired
    public AmqpService(AmqpTemplate amqpTemplate) {
        this.amqpTemplate = amqpTemplate;
    }

    /**
     * 发送消息处理逻辑
     *
     * @param area     区域
     * @param labelIds 标签Id列表
     * @param inc      变化值
     */
    public void sendAmqp(String area, List<String> labelIds, int inc) {
        if (area != null) {
            AreaItemIncVO areaItemIncVO = AreaItemIncVO.builder()
                    .areaName(area)
                    .inc(inc)
                    .build();
            amqpTemplate.convertAndSend(AmqpConstant.AreaConstant.AREA_ITEM_ROUTING_KEY, JsonUtil.toJson(areaItemIncVO));
        }
        if (labelIds != null && labelIds.size() > 0) {
            LabelItemIncVO labelItemIncVO = LabelItemIncVO.builder()
                    .labelIds(labelIds)
                    .inc(inc)
                    .build();
            amqpTemplate.convertAndSend(AmqpConstant.LabelConstant.LABEL_ITEM_ROUTING_KEY, JsonUtil.toJson(labelItemIncVO));
        }
    }
}
